package com.example.human_resource_management.mapper;

import com.example.human_resource_management.entry.dto.EmployeeDto;
import com.example.human_resource_management.entry.dto.PageDto;
import com.example.human_resource_management.entry.dto.PagesDto;
import com.example.human_resource_management.entry.po.Employee;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【employee(员工表)】的数据库操作Mapper
* @createDate 2025-06-09 08:48:41
* @Entity com.example.human_resource_management.entry.po.Employee
*/
public interface EmployeeMapper extends BaseMapper<Employee> {
    @Select("""
        SELECT
            e.emp_id, e.name, e.gender, e.contact,e.hire_date,
            d.dept_name,
            p.pos_name
        FROM
            employee e
            LEFT JOIN department d ON e.dept_id = d.dept_id
            LEFT JOIN position p ON e.pos_id = p.pos_id
        WHERE
            e.emp_id = #{empId}
        """)
    EmployeeDto selectEmployeeDetails(@Param("empId") Long empId);

    @Select("SELECT dept_id FROM employee WHERE emp_id = #{empId}")
    Long getDeptIdByEmpId(Long empId);


    @Select("SELECT e.emp_id, i.image, e.name, e.gender, d.dept_name, p.pos_name, e.contact, e.hire_date " +
            "FROM employee e " +
            "LEFT JOIN images i ON e.emp_id = i.emp_id " +
            "LEFT JOIN department d ON e.dept_id = d.dept_id " +
            "LEFT JOIN position p ON e.pos_id = p.pos_id " +
            "WHERE e.emp_id = #{empId}")
    PageDto getEmployeeInfo(Long empId);

    @Select("SELECT image FROM images WHERE emp_id = #{empId}")
    PagesDto getpages(Long empId);





    //管理员端
    @Select("""
        SELECT
            e.emp_id, e.name, e.gender, e.contact,
            d.dept_name,
            p.pos_name
        FROM
            employee e
            LEFT JOIN department d ON e.dept_id = d.dept_id
            LEFT JOIN position p ON e.pos_id = p.pos_id
        """)
    List<EmployeeDto> selectAllEmployeeDetails();

    @Insert("INSERT INTO employee (name, gender, dept_id, pos_id, hire_date, contact) VALUES (#{name}, #{gender}, #{deptId}, #{posId}, #{hireDate}, #{contact})")
    int insertEmployee(Employee employee);

    @Update("UPDATE employee SET name = #{name}, gender = #{gender}, dept_id = #{deptId}, pos_id = #{posId}, hire_date = #{hireDate}, contact = #{contact} WHERE emp_id = #{empId}")
    int updateEmployee(Employee employee);

    @Delete("DELETE FROM employee WHERE emp_id = #{empId}")
    int deleteEmployee(@Param("empId") Long empId);
}



